
//
//  WCheckgoddsCell.m
//  ProjectforHDX
//
//  Created by motom on 16/8/24.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "WCheckgoodsCell.h"
#import "PlateModel.h"


@interface WCheckgoodsCell()
{
    PlateModel *_plate;
}

@end

@implementation WCheckgoodsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}

#define cellFont CustomFont(14)

-(void)setPlateModle:(PlateModel *)plate
{
    self.plateName.text = nil;
    self.plateNo.text = nil;
    self.plateContain.text = nil;
    self.plateSeria.text = nil;
    self.plateNum.text = nil;
    _plate = (PlateModel *)plate;
    
    if ([plate.exception integerValue] == 1) {
        self.plateName.textColor = [UIColor redColor];
        self.plateNo.textColor = [UIColor redColor];
        self.plateContain.textColor = [UIColor redColor];
        self.plateSeria.textColor = [UIColor redColor];
    }else{
        self.plateName.textColor = [UIColor blackColor];
        self.plateNo.textColor = [UIColor blackColor];
        self.plateContain.textColor = [UIColor blackColor];
        self.plateSeria.textColor = [UIColor blackColor];
    }
    
    CGFloat height = 21;
    CGFloat width =  SCREEN_WIDTH / 6;
    if (plate.name != nil) {
        NSAttributedString *name = [[NSAttributedString alloc]initWithString:plate.name attributes:@{NSFontAttributeName:cellFont}];
        self.plateName.attributedText = name;
        CGSize sizeName = [self rectWithSize:plate.name andSize:CGSizeMake(0.8*width, MAXFLOAT)];
        height = sizeName.height;
        [self.plateName setFrame:CGRectMake(0, 0, sizeName.width, sizeName.height)];
    }
   
    
    if (plate.code != nil) {
        NSAttributedString *plateNo = [[NSAttributedString alloc]initWithString:plate.code attributes:@{NSFontAttributeName:cellFont}];
        self.plateNo.attributedText = plateNo;
        CGSize sizeNo = [self rectWithSize:plate.code andSize:CGSizeMake(1.2*width, MAXFLOAT)];
        if (sizeNo.height > height) {
            height = sizeNo.height;
        }
        [self.plateNo setFrame:CGRectMake(0, 0, sizeNo.width, sizeNo.height)];
    }
    
    if (plate.sn) {
        NSAttributedString *plateSn = [[NSAttributedString alloc]initWithString:plate.sn attributes:@{NSFontAttributeName:cellFont}];
        self.plateSeria.attributedText = plateSn;
        CGSize sizeSer = [self rectWithSize:plate.sn andSize:CGSizeMake(1.2*width, MAXFLOAT)];
        if (sizeSer.height > height) {
            height = sizeSer.height;

        }
        [self.plateSeria setFrame:CGRectMake(0, 0, sizeSer.width, sizeSer.height)];
    }

    
    if (plate.containerNo) {
        NSAttributedString *plateCont = [[NSAttributedString alloc]initWithString:plate.containerNo attributes:@{NSFontAttributeName:cellFont}];
        self.plateContain.attributedText = plateCont;
        CGSize sizeCon = [self rectWithSize:plate.containerNo andSize:CGSizeMake(0.8*width, MAXFLOAT)];
        if (sizeCon.height > height) {
            height = sizeCon.height;
        }
        
        [self.plateContain setFrame:CGRectMake(0, 0, sizeCon.width, sizeCon.height)];
 
    }
    
    self.plateNum.text = plate.number;
    self.height = height+15;
    
}



-(CGSize)rectWithSize:(NSString *)string andSize:(CGSize)size
{
    CGSize newSize = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;
    return newSize;

}

-(void)setupView
{
    RZCustomLable *plateName = [[RZCustomLable alloc]init];
    self.plateName = plateName;
    [self addSubview:plateName];
    
    RZCustomLable *plateNo = [[RZCustomLable alloc]init];
    self.plateNo = plateNo;
    [self addSubview:plateNo];
    
    RZCustomLable *plateSeria = [[RZCustomLable alloc]init];
    self.plateSeria = plateSeria;
    [self addSubview:plateSeria];
    
    RZCustomLable *plateContain = [[RZCustomLable alloc]init];
    self.plateContain = plateContain;
    [self addSubview:plateContain];
    
    UITextField *plateNum = [[UITextField alloc]init];
    plateNum.borderStyle = UITextBorderStyleLine;
    plateNum.inputAccessoryView = [[UIView alloc] init];
    plateNum.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.plateNum = plateNum;
    [self addSubview:plateNum];
    plateNum.backgroundColor = [UIColor whiteColor];
    
    
}

#define topMarin 10
-(void)layoutSubviews
{
    
    CGFloat width =  SCREEN_WIDTH / 5;
    self.plateNo.left = width;
    self.plateSeria.left = 2 *width;
    self.plateContain.left = 3.1 *width;
    self.plateNum.left = 3.95 *width;
    self.plateNum.width = 50;
    self.plateNum.height = 25;
    
    self.plateName.top = topMarin - 1;
    self.plateName.left = 10;
    
    
    self.plateNo.top = self.plateName.top;
    
    self.plateSeria.top = self.plateName.top;
    
    self.plateContain.top  = self.plateName.top;
    
    self.plateNum.top = self.plateName.top;
    
}

@end
